<?php
namespace Home\Controller;
// namespace Vendor;
use Think\Controller;
// use Vendor;
	 
class IndexController extends Controller 
{
	//网页授权
	public function login()
	{
		//用户同意授权，获取code
		$appID=C("appID");
		$appsecret=C("appsecret");
		$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appID&redirect_uri=https://www.1302am.com/guitar/index.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		header('Location:'.$url);
		
	}
	
	public function index()
	{
		/*获取用户信息*/
		$access_token =getAccessToken();
		// var_dump($result);exit();
		$appID=C("appID");
		$appSecret=C("appSecret");
		
		vendor("jssdk");
		$jssdk = new \Vendor\JSSDK($appID, $appSecret);
		// var_dump($jssdk);exit();
		$signPackage = $jssdk->GetSignPackage();
		// var_dump($signPackage);exit();
		
		//获取头像
		if(!empty($_GET['code']))
		{
			//通过code换取网页授权access_token
			$appID=C("appID");
			$appsecret=C("appsecret");
			$code=$_GET['code'];
			$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appID&secret=$appsecret&code=$code&grant_type=authorization_code";
			$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $return = curl_exec($ch);
			$data=json_decode($return);
			// var_dump($data);exit();
			
			$openid=$data->openid;
			$access_token=$data->access_token;
			$refresh_token=$data->refresh_token;
			
			//刷新access_token
			$url="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$appID&grant_type=refresh_token&refresh_token=$refresh_token";
			$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
			$data=json_decode($data);
			$access_token=$data->access_token;
			// var_dump($access_token);exit();
			
			$url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
			$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $return = curl_exec($ch);
			$userinfo=json_decode($return);
			// var_dump($userinfo);exit();
			$this->assign("userinfo",$userinfo);
		}
		
		$this->assign("signPackage",$signPackage);
		$this->display();

	}

	public function share()
	{
		vendor("jssdk");
		$appID=C("appID");
		$appsecret=C("appsecret");
		
		$jssdk = new \Vendor\JSSDK($appID, $appsecret);
		// var_dump($jssdk);exit();
		// var_dump($jssdk);exit();
		$signPackage = $jssdk->GetSignPackage();
		// var_dump($signPackage);exit();
		
		$this->assign("headimgurl",$_GET['headimgurl']);
		$this->assign("nickname",$_GET['nickname']);
		// var_dump($signPackage);exit();
		$this->assign("signPackage",$signPackage);
		$this->display();
	}

	public function record()
	{
		/*获取用户信息*/
		$access_token =getAccessToken();
		
		vendor("jssdk");
		$appID=C("appID");
		$appsecret=C("appsecret");
		$jssdk = new \Vendor\JSSDK($appID, $appsecret);
		// var_dump($jssdk);exit();
		$signPackage = $jssdk->GetSignPackage();
		// var_dump($signPackage);exit();
		$this->assign("signPackage",$signPackage);
		$this->display();
	}

	public function verify()
	{
		wechatLog($_GET);

		// $_GET['echostr']="4553980651907763185";
		// $_GET['signature']="73fa5e776cf609c26ef7b0c30cc2e53cac2bc247";
		// $_GET['timestamp']="1454803392";
		// $_GET['nonce']="1187747471";
		// var_dump($_GET);exit();
		
		$echostr = $_GET['echostr'];		 // 随机字符串 
		$signature = $_GET['signature'];	//微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
		$timestamp = $_GET['timestamp'];
		$nonce = $_GET['nonce'];			// 随机数 
		$token="xiongmaoguitar";
		
		vendor('Wechat');
		$wechatObj = new \Vendor\Wechat($echostr,$signature,$timestamp,$nonce,$token);
		
		$data = $wechatObj->valid();
		var_dump($data);exit();

	}

	public function scan()
	{
		$this->display();
	}
	
	public function phpinfo()
	{
		phpinfo();
	}

	public function publish(){
		$this->display();
	}

	public function recordList(){
		$this->display();
	}
}